package com.fun_ordering.service.impl;

import com.fun_ordering.service.OrderDetailService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.fun_ordering.components.PageResult;
import com.fun_ordering.mapper.OrderDetailMapper;
import com.fun_ordering.pojo.OrderDetail;
import com.fun_ordering.pojo.OrderDetailExample;
import com.fun_ordering.pojo.OrderDetailExample.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 订单菜品详细表服务实现层
 * @author XiaoPi
 *
 */
@Service
public class OrderDetailServiceImpl implements OrderDetailService {

	@Autowired
	private OrderDetailMapper orderDetailMapper;
	
	/**
	 * 查询全部
	 */
	@Override
	public List<OrderDetail> findAll() {
		return orderDetailMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<OrderDetail> page=   (Page<OrderDetail>) orderDetailMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(OrderDetail orderDetail) {
		orderDetailMapper.insert(orderDetail);		
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(OrderDetail orderDetail){
		orderDetailMapper.updateByPrimaryKey(orderDetail);
	}	
	
	/**
	 * 根据ID获取实体
	 * @param orderDetailId
	 * @return
	 */
	@Override
	public OrderDetail findOne(Integer orderDetailId){
		return orderDetailMapper.selectByPrimaryKey(orderDetailId);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Integer[] orderDetailIds) {
		for(Integer orderDetailId:orderDetailIds){
			orderDetailMapper.deleteByPrimaryKey(orderDetailId);
		}		
	}
	
	
		@Override
	public PageResult findPage(OrderDetail orderDetail, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		OrderDetailExample example=new OrderDetailExample();
		Criteria criteria = example.createCriteria();
		
		if(orderDetail!=null){			
						if(orderDetail.getDishName()!=null && orderDetail.getDishName().length()>0){
				criteria.andDishNameLike("%"+orderDetail.getDishName()+"%");
			}	
		}
		
		Page<OrderDetail> page= (Page<OrderDetail>)orderDetailMapper.selectByExample(example);
		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public List<OrderDetail> findOneByOrderId(Integer orderId) {
		return orderDetailMapper.findAllByOrderId(orderId);
	}

}
